﻿var pageSize = 15;
var JourPIDJson;
$(document).ready(function () {
    //按照PID进行 查询
    $.ajax({
        type: "post",
        data: { action: "GetListByPID" },
        dataType: "json",
        url: "../../Ajax/CommunicationAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                JourPIDJson = data.json;
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });

    PageClickFunc(1);
    $("#btnSele").click(function () {
        InitUserList(1)
    });

    $("#btnAdd").click(function () {
        Edit(null);
    })

    $("#txtIMSI").focus(function () {
        if ($(this).val() == "输入IMSI查询") {
            $(this).val("");
        }
    })

    $("#txtIMSI").blur(function () {
        if ($(this).val() == "") {
            $(this).val($(this).attr("default"));
        }

    })

    $("#txtUserAccount").focus(function () {
        if ($(this).val() == "输入用户账号") {
            $(this).val("");
        }
    })

    $("#txtUserAccount").blur(function () {
        if ($(this).val() == "") {
            $(this).val($(this).attr("default"));
        }

    })

    $("#txtAccount").focus(function () {
        if ($(this).val() == "输入用户系统账号") {
            $(this).val("");
        }
    })

    $("#txtAccount").blur(function () {
        if ($(this).val() == "") {
            $(this).val($(this).attr("default"));
        }
    })
})
PageClickFunc = function (pageclickednumber) {
    InitUserList(pageclickednumber);
}
function InitUserList(pageclickednumber) {
    var IMSI = $("#txtIMSI").val() == $("#txtIMSI").attr("default") ? "" : $("#txtIMSI").val();
    var sysAccount = $("#txtAccount").val() == $("#txtAccount").attr("default") ? "" : $("#txtAccount").val();
    var userAccount = $("#txtUserAccount").val() == $("#txtUserAccount").attr("default") ? "" : $("#txtUserAccount").val();

    $.ajax({
        type: "post",
        data: { action: "GetList", pageIndex: pageclickednumber, pageSize: pageSize, IMSISELE: IMSI, sysAccount: sysAccount, userAccount: userAccount },
        datatype: "json",
        url: "../../Ajax/UserAjax.ashx",
        beforSend: function () {
        },
        success: function (data) {
            if (data != null) {
                var json = $.parseJSON(data).json;
                var tbody = "";
                var pageCount = $.parseJSON(data).flag;
                $.each(json, function (k, v) {
                    var stateName = "";
                   
                    switch (v.State) {
                        case "1":
                            stateName = "正常";
                            break;
                        case "2":
                            stateName = "黑名单";
                            break;
                        default:
                            stateName = "正常";
                            break;
                    }
                    tbody += "<tr>"
                    tbody += "<td><lable name='lblId' id='lbl" + k + "'>";
                    tbody += v.ID;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblAcccount'>";
                    tbody += v.UserAccount;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblUserName' id='" + v.UserInfoID + "'>";
                    tbody += v.UserName;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblCommunicationID' id='" + v.Pid + "'>";
                    tbody += v.PName;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblIsRobot' >";
                    tbody += v.IsRobot;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblState' id='" + v.State + "'>";
                    tbody += stateName
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblRegisterTime'>";
                    tbody += v.RegisterTime;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblIMSI'>";
                    tbody += v.IMSI;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblIMEI'>";
                    tbody += v.IMEI;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblNumber'>";
                    tbody += v.Number;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblArea'>";
                    tbody += v.Area;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblPlmn'>";
                    tbody += v.Plmn;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblMac'>";
                    tbody += v.Mac;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblInviteUserID'>";
                    tbody += v.InviteUserID;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblInviteScore'>";
                    tbody += v.InviteScore;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblInviteTotalScore'>";
                    tbody += v.InviteTotalScore;
                    tbody += "</lable></td>"; 
                    tbody += "<td><lable name='lblInviteSuccessNum'>";
                    tbody += v.InviteSuccessNum; 
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblInviteTime'>";
                    tbody += v.InviteTime == "0001/1/1 0:00:00" ? "" : v.InviteTime;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblPrice'>";
                    tbody += v.Price;
                    tbody += "</lable></td>";
                    tbody += "<td>";
                    tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                    if (v.IsRobot == "True")
                        tbody += "<input type='button' onclick='UpdateUserName(this)' value='更改用户名'>"
                    tbody += "</td>";
                    tbody += "</tr>"
                })
                $("#tb1").empty().append(tbody);
                $("#pager").pager({ pagenumber: pageclickednumber, pagecount: Math.ceil(pageCount / pageSize), buttonClickCallback: PageClickFunc });
            }
        },
        complete: function () {
        },
        error: function () {
        }
    })
}

function UpdateUserName(contr) {
    var userInfoID = $(contr).parent().parent().find("lable[name=lblUserName]").first().attr("id");
    var userName = $(contr).parent().parent().find("lable[name=lblUserName]").first().text();
    var content = "<table><tr><td>用户名：<input type='text' id='txtUserName' value='" + userName + "' style='width:200px;float:right' /></td></tr>"
    $.dialog({
        title: "更改用户名",
        content: content,
        button: [
                    {
                        name: "提交",
                        callback: function () {
                            userName = $("#txtUserName").val();
                            $.ajax({
                                type: "post",
                                data: { action: "EditUserName", userInfoID: userInfoID, userName: userName },
                                datatype: "json",
                                url: "../../Ajax/UserAjax.ashx",
                                beforSend: function () {
                                },
                                success: function (data) {
                                    if (data == "1") {
                                        $.dialog.tips("更新成功！", 1);
                                        $(contr).parent().parent().find("lable[name=lblUserName]").first().text(userName)
                                    }
                                    else {
                                        $.dialog.tips("更新失败！", 1);
                                    }
                                },
                                complete: function () {
                                },
                                error: function () {
                                }
                            })
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false
    });
}



function Update(contr) {
    var lbl = $(contr).parent().parent().find("lable[name=lblId]").first();
    Edit(lbl);
}
function InitSelectPid(value) {
    var sel = "";
    $.each(JourPIDJson, function (k, v) {
        if ((value == null && k == 0) || v.ID == value)
            sel += "<option value='" + v.ID + "' selected='selected'>" + v.Name + "</option>";
        else
            sel += "<option value='" + v.ID + "'>" + v.Name + "</option>";
    })
    return sel;
}

function Edit(lbl) {
    //lbl 是判断是添加还是修改  添加为null
    var sortNum = $(lbl).parent().parent().find("lable[name=lblName]");
    var sortId = $(lbl).parent().parent().find("lable[name=lblId]").text();
    var stateID = $(lbl).parent().parent().find("lable[name=lblState]").attr("id");
    var content = "";
    var data = new Date();
    if (lbl != null) {
        content += "<table><tr><td>所属渠道id：<select id='selPID' disabled='disabled'>" + InitSelectPid($(lbl).parent().parent().find("lable[name=lblCommunicationID]").attr("id")) + "</select></td></tr>"
        if (stateID == "1")
            content += "<tr><td>用户状态： <select id='selState'  style='width:200px;float:right' ><option value='1' selected='selected'>正常</option><option value='2'>黑名单</option><option value='3'>任务奖品不显示</option></select></td></tr>"
        else if (stateID == "2")
            content += "<tr><td>用户状态： <select id='selState'  style='width:200px;float:right' ><option value='1' >正常</option><option value='2' selected='selected'>黑名单</option><option value='3'>任务奖品不显示</option></select></td></tr>"
        else if (stateID == "3")
            content += "<tr><td>用户状态： <select id='selState'  style='width:200px;float:right' ><option value='1' >正常</option><option value='2' >黑名单</option><option value='3' selected='selected'>任务奖品不显示</option></select></td></tr>"
        content += "<tr><td>用户手机IMSI：<input type='text' id='txtIMSI' value='" + $(lbl).parent().parent().find("lable[name=lblIMSI]").text() + "' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>手机的imei号码：<input type='text' id='txtIMEI' value='" + $(lbl).parent().parent().find("lable[name=lblIMEI]").text() + "' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>sim卡号码：<input type='text' id='txtNumber' value='" + $(lbl).parent().parent().find("lable[name=lblNumber]").text() + "' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>sim卡短信中心号码：<input type='text' id='txtArea' value='" + $(lbl).parent().parent().find("lable[name=lblArea]").text() + "' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>sim卡的plmn号码：<input type='text' id='txtPlmn' value='" + $(lbl).parent().parent().find("lable[name=lblPlmn]").text() + "' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>手机mac地址：<input type='text' id='txtMac' value='" + $(lbl).parent().parent().find("lable[name=lblMac]").text() + "' style='width:200px;float:right' /></td></tr></table>"


    }
    else {
        content += "<table><tr><td>所属渠道id：<select id='selPID' >" + InitSelectPid() + "</select></td></tr>"
        content += "<tr><td>是否是机器人：<input type='radio' name='radio' id='radioisTure' value='true'/><lable>是</lable><input type='radio' name='radio' id='radioisfalse' value='false'/><lable>否</lable></td></tr>"
        content += "<tr><td>用户状态： <select id='selState'  style='width:200px;float:right' ><option value='1' selected='selected'>正常</option><option value='2'>黑名单</option><option value='3'>任务奖品不显示</option></select></td></tr>"
        content += "<tr><td>用户手机IMSI：<input type='text' id='txtIMSI' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>手机的imei号码：<input type='text' id='txtIMEI' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>sim卡号码：<input type='text' id='txtNumber' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>sim卡短信中心号码：<input type='text' id='txtArea' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>sim卡的plmn号码：<input type='text' id='txtPlmn' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>手机mac地址：<input type='text' id='txtMac' style='width:200px;float:right' /></td></tr></table>"
        //        content += "</br><tr><td>对应：<input type='text' id='txtChanceTypeID' style='width:200px' /></td></tr>"
        //        content += "<tr><td>系统创建用户账号：<input type='text' id='txtUserAccount' style='width:200px' /></td></tr>"
    }

    $.dialog({
        title: "更新用户设置模板",
        content: content,
        button: [
                    {
                        name: "提交",
                        callback: function () {
                            var objectCommunicationID = $("#selPID").val();
                            var objectIsRobot = $("#txtIsRobot").val();
                            var objectState = $("#selState").val();
                            var objectIMSI = $("#txtIMSI").val();
                            var objectIMEI = $("#txtIMEI").val();
                            var objectNumber = $("#txtNumber").val();
                            var objectArea = $("#txtArea").val();
                            var objectPlmn = $("#txtPlmn").val();
                            var objectMac = $("#txtMac").val();
                            AddMould(objectCommunicationID, lbl, $("#selPID"), sortId);
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false
    });
}

function AddMould(objectName, contr, sortNum, sortId) {
    var objectId = sortId;
    var numVal = $(sortNum).val();
    var objectCommunicationID = $("#selPID").val();
    //是否启用账号
    var objectIsRobot = $('input[name=radio]:checked').val();
    var objectState = $("#selState").val();
    var objectStateValue = $("#selState>option:selected").text();
    var objectIMSI = $("#txtIMSI").val();
    var objectIMEI = $("#txtIMEI").val();
    var objectNumber = $("#txtNumber").val();
    var objectArea = $("#txtArea").val();
    var objectPlmn = $("#txtPlmn").val();
    var objectMac = $("#txtMac").val();
    numVal = numVal == "" ? 0 : numVal;
    var data = contr == null ? { action: "Add", CommunicationID: objectCommunicationID, IsRobot: objectIsRobot, State: objectState, IMSI: objectIMSI, IMEI: objectIMEI, Number: objectNumber, Area: objectArea, Plmn: objectPlmn, Mac: objectMac} :
              { action: "Update", id: objectId, CommunicationID: objectCommunicationID, State: objectState, IMSI: objectIMSI, IMEI: objectIMEI, Number: objectNumber, Area: objectArea, Plmn: objectPlmn, Mac: objectMac };
              $.ajax({
                  type: "post",
                  data: data,
                  async: true,
                  url: "../../Ajax/UserAjax.ashx",
                  beforeSend: function () {
                  },
                  success: function (data) {

                      $.dialog.tips("更新成功！", 1);
                      if (contr == null) {
                          var tbody = "<tr>";
                          tbody += "<td><lable name='lblId'>";
                          tbody += data;
                          tbody += "</lable></td>";
                          tbody += "<td><lable name='lblAcccount'>";
                          tbody += "</lable></td>";
                          tbody += "<td><lable name='lblCommunicationID'>";
                          tbody += objectCommunicationID;
                          tbody += "</lable></td>";
                          tbody += "<td><lable name='lblIsRobot'>";
                          tbody += objectIsRobot;
                          tbody += "</lable></td>";
                          tbody += "<td><lable name='lblState' id ='" + objectState + "'>";
                          tbody += objectStateValue;
                          tbody += "</lable></td>";
                          tbody += "<td><lable name='lblRegisterTime'>";
                          tbody += objectRegisterTime;
                          tbody += "</lable></td>";
                          tbody += "<td><lable name='lblIMSI'>";
                          tbody += objectIMSI;
                          tbody += "</lable></td>";
                          tbody += "<td><lable name='lblIMEI'>";
                          tbody += objectIMEI;
                          tbody += "</lable></td>";
                          tbody += "<td><lable name='lblNumber'>";
                          tbody += objectNumber;
                          tbody += "</lable></td>";
                          tbody += "<td><lable name='lblArea'>";
                          tbody += objectArea;
                          tbody += "</lable></td>";
                          tbody += "<td><lable name='lblPlmn'>";
                          tbody += objectPlmn;
                          tbody += "</lable></td>";
                          tbody += "<td><lable name='lblMac' >";
                          tbody += objectMac;
                          tbody += "</lable></td>";
                          tbody += "<td>";
                          tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                          tbody += "</td>";
                          tbody += "</tr>"
                          $("#tb1").append(tbody);
                      }
                      else {
                          $(contr).parent().parent().find("lable[name=lblId]").first().text(objectName);
                          $(contr).parent().parent().find("lable[name=lblCommunicationID]").first().text(objectCommunicationID);
                          $(contr).parent().parent().find("lable[name=lblIsRobot]").first().text(objectIsRobot);
                          $(contr).parent().parent().find("lable[name=lblState]").first().text(objectStateValue);
                          $(contr).parent().parent().find("lable[name=lblState]").first().attr("id", objectState);
                          $(contr).parent().parent().find("lable[name=lblRegisterTime]").first().text(objectRegisterTime);
                          $(contr).parent().parent().find("lable[name=lblIMSI]").first().text(objectIMSI);
                          $(contr).parent().parent().find("lable[name=lblIMEI]").first().text(objectIMEI);
                          $(contr).parent().parent().find("lable[name=lblNumber]").first().text(objectNumber);
                          $(contr).parent().parent().find("lable[name=lblArea]").first().text(objectArea);
                          $(contr).parent().parent().find("lable[name=lblPlmn]").first().text(objectPlmn);
                          $(contr).parent().parent().find("lable[name=lblIMac]").first().text(objectMac);
                      }
                  },
                  complete: function () {
                  },
                  error: function () {
                  }
              })
}
function Delete(contr) {
    var lbl = $(contr).parent().parent().find("lable[name=lblId]").first();
    if (window.confirm("确定删除？")) {
        $.ajax({
            type: "post",
            data: { action: "Delete", name: $(lbl).text() },
            async: true,
            url: "../../Ajax/UserAjax.ashx",
            beforeSend: function () {
            },
            success: function (data) {
                if (data == "True") {
                    $($(lbl).parent().parent()).remove();
                    $.dialog.tips("删除成功！", 1);
                }
                else {
                    $.dialog.tips("删除失败！", 1);
                }
            },
            complete: function () {
            },
            error: function () {
            }
        })
    }
}
